﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Enter.Common
{
    public sealed class SafeConvert
    {
        public static bool IsDBNull(object obj)
        {
            return Convert.IsDBNull(obj);
        }

        public static bool ToBoolean(object obj)
        {
            return ToBoolean(obj, false);
        }

        public static bool ToBoolean(string s)
        {
            return ToBoolean(s, false);
        }

        private static bool ToBoolean(object obj, bool defaultValue)
        {
            if (obj != null)
            {
                return ToBoolean(obj.ToString(), defaultValue);
            }
            return defaultValue;
        }

        private static bool ToBoolean(string s, bool defaultValue)
        {
            bool flag;
            return (bool.TryParse(s, out flag) ? flag : defaultValue);
        }

        public static DateTime ToDateTime(object obj)
        {
            return ToDateTime(obj, DateTime.MinValue);
        }

        public static DateTime ToDateTime(string s)
        {
            return ToDateTime(s, DateTime.MinValue);
        }

        public static DateTime ToDateTime(object obj, DateTime defaultValue)
        {
            if (obj != null)
            {
                return ToDateTime(obj.ToString(), defaultValue);
            }
            return defaultValue;
        }

        public static DateTime ToDateTime(string s, DateTime defaultValue)
        {
            DateTime time;
            return (DateTime.TryParse(s, out time) ? time : defaultValue);
        }

        public static decimal ToDecimal(object obj)
        {
            return ToDecimal(obj, 0M);
        }

        public static decimal ToDecimal(string s)
        {
            return ToDecimal(s, 0M);
        }

        public static decimal ToDecimal(object obj, decimal defaultValue)
        {
            if (obj != null)
            {
                return ToDecimal(obj.ToString(), defaultValue);
            }
            return defaultValue;
        }

        public static decimal ToDecimal(string s, decimal defaultValue)
        {
            decimal num;
            return (decimal.TryParse(s, out num) ? num : defaultValue);
        }

        public static double ToDouble(object obj)
        {
            return ToDouble(obj, 0.0);
        }

        public static double ToDouble(string s)
        {
            return ToDouble(s, 0.0);
        }

        public static double ToDouble(object obj, double defaultValue)
        {
            if (obj != null)
            {
                return ToDouble(obj.ToString(), defaultValue);
            }
            return defaultValue;
        }

        public static double ToDouble(string s, double defaultValue)
        {
            double num;
            return (double.TryParse(s, out num) ? num : defaultValue);
        }

        public static float ToFloat(object obj)
        {
            return ToFloat(obj, 0f);
        }

        public static float ToFloat(string s)
        {
            return ToFloat(s, 0f);
        }

        public static float ToFloat(object obj, float defaultValue)
        {
            if (obj != null)
            {
                return ToFloat(obj.ToString(), defaultValue);
            }
            return defaultValue;
        }

        public static float ToFloat(string s, float defaultValue)
        {
            float num;
            return (float.TryParse(s, out num) ? num : defaultValue);
        }

        public static int ToInt32(object obj)
        {
            return ToInt32(obj, 0);
        }

        public static int ToInt32(string s)
        {
            return ToInt32(s, 0);
        }

        public static int ToInt32(object obj, int defaultValue)
        {
            if (obj != null)
            {
                return ToInt32(obj.ToString(), defaultValue);
            }
            return defaultValue;
        }

        public static int ToInt32(string s, int defaultValue)
        {
            int num;
            return (int.TryParse(s, out num) ? num : defaultValue);
        }

        public static long ToInt64(object obj)
        {
            return ToInt64(obj, 0L);
        }

        public static long ToInt64(string s)
        {
            return ToInt64(s, 0L);
        }

        public static long ToInt64(object obj, long defaultValue)
        {
            if (obj != null)
            {
                return ToInt64(obj.ToString(), defaultValue);
            }
            return defaultValue;
        }

        public static long ToInt64(string s, long defaultValue)
        {
            long num;
            return (long.TryParse(s, out num) ? num : defaultValue);
        }

        public static float ToSingle(object obj)
        {
            return ToSingle(obj, 0f);
        }

        public static float ToSingle(string s)
        {
            return ToSingle(s, 0f);
        }

        public static float ToSingle(object obj, float defaultValue)
        {
            if (obj != null)
            {
                return ToSingle(obj.ToString(), defaultValue);
            }
            return defaultValue;
        }

        public static float ToSingle(string s, float defaultValue)
        {
            float num;
            return (float.TryParse(s, out num) ? num : defaultValue);
        }

        public static string ToString(object obj)
        {
            if (obj != null)
            {
                return obj.ToString();
            }
            return string.Empty;
        }

        public static string ToString(string s)
        {
            return ToString(s, string.Empty);
        }

        public static string ToString(string s, string defaultString)
        {
            if (s == null)
            {
                return defaultString;
            }
            return s.ToString();
        }
    }
}
